import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class RedisClient {
    public static void main(String[] args) {
        Socket socket = null;// 1.建立一个与服务端之间的连接
        OutputStream output = null;// 用于向别人发消息
        InputStream inputStream=null;
        try {
            socket = new Socket("127.0.0.1", 6379);
            // 2.将需要发送的数据写入到网络中，注意：包含了两个流：InputStream和OutputStream
            output = socket.getOutputStream();
            output.write("*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$7\r\nabcdefg\r\n".getBytes());// 3.写入
            output.flush();
            inputStream=socket.getInputStream();
            // 4.收取服务端发送来的消息 new BufferedInputStream(socket.getInputStream());
            byte[] arr = new byte[1024];
            int len = inputStream.read(arr);
            String message = new String(arr, 0, len);
            System.out.println(message);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                output.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
